package com.shenglue.ssi.action;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.shenglue.ssi.domain.BuyBackDetail;
import com.shenglue.ssi.domain.BuyBackMain;
import com.shenglue.ssi.domain.Good;
import com.shenglue.ssi.service.BuyBackDetailService;
import com.shenglue.ssi.service.BuyBackMainService;
import com.shenglue.ssi.service.GoodService;

public class BuyBackDetailAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private BuyBackMain buyBackMain;		// 主表记录的 Bean
	private BuyBackDetail buyBackDetail;	// 对应的 Bean
	private int buyBackMainId;			// 所属的 主表记录 之Id
	private int buyBackDetailId;			// 删除/修改 的 记录之Id
	private BuyBackMainService   buyBackMainService;   // 依赖的 Service（辅）
	private GoodService		   goodService;   	   // 依赖的 Service（辅）
	private BuyBackDetailService buyBackDetailService; // 依赖的 Service（主）

	/**
	 * 查询所有入库退货单子表信息方法
	 */
	public String queryAll() throws Exception {
		List<BuyBackDetail> list = buyBackDetailService.selectAllBuyBackDetail( buyBackMainId );
		if( list == null ) {
			// 其实不用创建，后面的JSP 够聪明  list = new ArrayList<BuyBackDetail>();
			// 不能认为是错误，因为一开始是没有明细的  return INPUT;
		}

		buyBackMain = buyBackMainService.getOneBuyBackMainForUpdate( buyBackMainId );
		if( buyBackMain == null ) {
			return INPUT;
		}

		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute( "buyBackDetailList", list );
		request.setAttribute( "buyBackMain", buyBackMain );

		return SUCCESS;
	}

	/**
	 * 增加入库退货单子表记录方法
	 */
	public String addNew() throws Exception {
		boolean flag = buyBackDetailService.saveBuyBackDetail( buyBackDetail );
		if(flag==true){
			return SUCCESS;
		}else{
			// 这2句 ，无法传递给 JSP，只能另想它法
			// HttpServletRequest request = ServletActionContext.getRequest();
			// request.setAttribute( "error", "增加明细信息失败，请检查商品信息是否重复" );

			errMsg = "增加明细信息失败，请检查商品信息是否重复";
			// 此句移到 errMsg 的 get 方法中了 ：errMsg = java.net.URLEncoder.encode( errMsg, "utf-8" );
			return ERROR;
		}
	}

	/**
	 * 删除入库退货单子表记录方法
	 */
	public String delete() throws Exception {

		boolean flag = buyBackDetailService.deleteBuyBackDetail( buyBackDetailId );
		if( flag == true ) {
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

	/**
	 * 修改入库退货单子表记录方法
	 */
	public String update() throws Exception {

		boolean flag = buyBackDetailService.updateBuyBackDetail( buyBackDetail );
		if( flag == true ) {
			return SUCCESS;
		}else{
			// errMsg = "修改明细信息失败，试一下能否显示出来——能显示";
			return ERROR;
		}

	}

	/**
	 * 准备 转向 新增 入库退货单子表记录 的界面
	 */
	public String prepareAddBuyBackDetail() {
		boolean flag = selectGoodOf1Supp();
		if( flag == true ) {
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

	/**
	 * 准备 转向 修改 入库退货单子表记录 的界面
	 */
	public String prepareModBuyBackDetail() {
		// 应先执行 这一段 取得 buyBackDetail， 以 顺便获得 buyBackMainId。因为 调本方法 的 url 不传递 buyBackMainId 进来
		buyBackDetail = buyBackDetailService.getOneBuyBackDetailForUpdate( buyBackDetailId );
		if( buyBackDetail == null ) {
			return ERROR;
		}

		// 由于不可修改 商品，所以下面的 查 Good 的代码 不需要了
		// buyBackMainId = buyBackDetail.getBuyBackId(); // 然后才能调  selectGoodOf1Supp()

		// boolean flag = selectGoodOf1Supp();
		// if( flag == false ) {
		// 	return ERROR;
		// }

		return SUCCESS;
	}

	/**
	 * 预先查好 商品，为了制作下拉列表框   —— 当前入库退货单 有且仅有一个供应商，查该供应商的 所有商品
	 */
	private boolean selectGoodOf1Supp() {

		// 先从 buyBackMainId 取得 buyBackMain实例，再从中取得 suppId
		buyBackMain = buyBackMainService.getOneBuyBackMainForUpdate( buyBackMainId );
		if( buyBackMain == null ) {
			return false;
		}
		int suppId = buyBackMain.getSuppId();

		// 根据取得的 suppId，得到 currSuppAllGood，置于 request
		boolean bRtn;
		List<Good> currSuppAllGood = goodService.selectGoodOf1Supp( suppId );
		if( currSuppAllGood != null ){
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute( "currSuppAllGood", currSuppAllGood );
			bRtn = true;
		}else{
			bRtn = false;
		}
		return bRtn;
	}

	public BuyBackDetailAction() {
	}

	public BuyBackMain getBuyBackMain() {
		return buyBackMain;
	}

	public void setBuyBackMain(BuyBackMain buyBackMain) {
		this.buyBackMain = buyBackMain;
	}

	public BuyBackDetail getBuyBackDetail() {
		return buyBackDetail;
	}

	public void setBuyBackDetail(BuyBackDetail buyBackDetail) {
		this.buyBackDetail = buyBackDetail;
	}

	public int getBuyBackMainId() {
		return buyBackMainId;
	}

	public void setBuyBackMainId(int buyBackMainId) {
		this.buyBackMainId = buyBackMainId;
	}

	public int getBuyBackDetailId() {
		return buyBackDetailId;
	}

	public void setBuyBackDetailId(int buyBackDetailId) {
		this.buyBackDetailId = buyBackDetailId;
	}


	public BuyBackMainService getBuyBackMainService() {
		return buyBackMainService;
	}

	public void setBuyBackMainService(BuyBackMainService buyBackMainService) {
		this.buyBackMainService = buyBackMainService;
	}

	public GoodService getGoodService() {
		return goodService;
	}

	public void setGoodService(GoodService goodService) {
		this.goodService = goodService;
	}

	public BuyBackDetailService getBuyBackDetailService() {
		return buyBackDetailService;
	}

	public void setBuyBackDetailService(BuyBackDetailService buyBackDetailService) {
		this.buyBackDetailService = buyBackDetailService;
	}

}
